package guij.controller.subscriber;

import guij.dao.mapper.ItemMapper;
import guij.taobao.TopInterface;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;
import com.taobao.api.model.Item;

//获取某个nick最新的RSS
public class ShowRSS implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {

		String nick = arg0.getParameter("nick");
		List<Item> items = itemMapper.selectByNick(nick);
		
	arg1.setContentType("text/xml; charset=UTF-8");
 
 	SyndFeed feed = new SyndFeedImpl();
	feed.setFeedType("rss_2.0");//指定RSS版本
	feed.setTitle("");
	feed.setLink("");
	feed.setDescription("");
 
	List<SyndEntry> entries=new ArrayList<SyndEntry>();
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
 
	for(int i = 0;i<items.size();i++){
	SyndEntry entry=new SyndEntryImpl();
	SyndContent content=new SyndContentImpl();
 
	entry.setTitle(items.get(i).getTitle());
	entry.setLink(items.get(i).getDetailUrl());
	entry.setPublishedDate(items.get(i).getCreated());
	entry.setAuthor(items.get(i).getNick());
 
	content.setType("text/plain");;
	content.setValue("");
	entry.setDescription(content);
 
	entries.add(entry);
	}
	feed.setEntries(entries);
	SyndFeedOutput put=new SyndFeedOutput();
	put.output(feed,arg1.getWriter());
	return null;
	}
	public void setViewPage(String viewPage) {
		this.viewPage = viewPage;
	}

	public String getViewPage() {
		return viewPage;
	}

	private String viewPage;
	public void setItemMapper(ItemMapper itemMapper) {
		this.itemMapper = itemMapper;
	}
	public ItemMapper getItemMapper() {
		return itemMapper;
	}
	private ItemMapper itemMapper;
}
